package com.zucai365.football.service.impl;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.wcj.common.service.impl.WcjServiceImpl;
import com.zucai365.football.bean.FootballTeamBean;
import com.zucai365.football.bean.LianSaiBean;
import com.zucai365.football.bean.LianSaiYearBean;
import com.zucai365.football.bean.TeamDZBean;
import com.zucai365.football.dao.TeamDZDao;
import com.zucai365.football.service.FootballTeamService;
import com.zucai365.football.service.TeamDZService;
import com.zucai365.football.utils.DateUtils;

@Service("teamDZService")
public class TeamDZServiceImpl extends WcjServiceImpl<TeamDZBean> implements TeamDZService{
	
	@Autowired
	@Qualifier("teamDZDao")
	private TeamDZDao teamDZDao;

	public TeamDZDao getTeamDZDao() {
		return teamDZDao;
	}

	public void setTeamDZDao( TeamDZDao teamDZDao ) {
		this.teamDZDao = teamDZDao;
	}
	
	@Autowired
	@Qualifier("footballTeamService")
	private FootballTeamService footballTeamService;
	
	public FootballTeamService getFootballTeamService() {
		return footballTeamService;
	}

	public void setFootballTeamService(FootballTeamService footballTeamService) {
		this.footballTeamService = footballTeamService;
	}

	@Override
	public void saveTeamDZ(LianSaiBean lianSai, LianSaiYearBean lianSaiYear,
			List<String> teamDzList) {
		
		List<TeamDZBean> teamDZList = new ArrayList<TeamDZBean>();
		
		for( String teamDZJson : teamDzList ){
			String[] json = teamDZJson.split( "=" );
			
			String lunciData = json[0].trim();
			
			//jh["R_7"]
			String lunciTemp = lunciData.split( "_" )[1];
			int flag = lunciTemp.indexOf( "\"" );
			int lunci = Integer.parseInt( lunciTemp.substring( 0,flag ) );
			
			String dzData =json[1].trim();
			dzData = dzData.substring(0,dzData.length()-1 );
			
			JSONArray jsonArray = JSONArray.fromObject( dzData );
			for( int i=0;i<jsonArray.size();i++ ){
				JSONArray teamDZArray = (JSONArray)jsonArray.get(i);
				
				String playId = teamDZArray.getString(0);
				String playTime = teamDZArray.getString(3);
				
				Long zdBet007Id = teamDZArray.getLong(4);
				Long kdBet007Id = teamDZArray.getLong(5);
				
				FootballTeamBean zdTeam = this.footballTeamService.getFootballTeamByBet007Id( zdBet007Id );
				FootballTeamBean kdTeam = this.footballTeamService.getFootballTeamByBet007Id( kdBet007Id );
				
				//http://bf.bet007.com/detail/715137cn.htm
				
				TeamDZBean teamDZ = this.getTeamDZBean( lianSai.getId(), lianSaiYear.getId(), zdTeam.getId(), kdTeam.getId(), lunci );
				if( teamDZ == null ){
					teamDZ = new TeamDZBean();
				}
				
				teamDZ.setPlayId(playId);
				teamDZ.setLiansai( lianSai );
				teamDZ.setLiansaiYear( lianSaiYear );
				teamDZ.setLunci( lunci );
				teamDZ.setPlayTime( DateUtils.formatTimestamp( playTime ) );
				teamDZ.setZd( zdTeam );
				teamDZ.setZdName( zdTeam.getFootballTeamName() );
				teamDZ.setKd( kdTeam );
				teamDZ.setKdName( kdTeam.getFootballTeamName() );
				teamDZ.setUrlBet007( "http://bf.bet007.com/detail/"+playId+"cn.htm" );
							
				teamDZList.add( teamDZ );
				
			}
		}
		
		this.teamDZDao.saveAll( teamDZList );
	}
	
	
	@Override
	public TeamDZBean getTeamDZBean( Long lianSaiId,Long lianSaiYearId, Long zdId, Long kdId,int lunci ) {

		DetachedCriteria criteria = DetachedCriteria.forClass( TeamDZBean.class, "teamDZ" );
		criteria.add( Restrictions.eq( "teamDZ.liansaiYear.id", lianSaiYearId ) );
		criteria.add( Restrictions.eq( "teamDZ.liansai.id", lianSaiId ) );
		criteria.add( Restrictions.eq( "teamDZ.zd.id", zdId ) );
		criteria.add( Restrictions.eq( "teamDZ.kd.id", kdId ) );
		criteria.add( Restrictions.eq( "teamDZ.lunci", lunci ) );
		
		List<TeamDZBean> list = this.teamDZDao.findByCriteria(criteria);
		if( list.size() > 1){
			throw new RuntimeException( "每只球队没轮对阵记录插入错误！" );
		}
		
		if( list.isEmpty() ){
			return null;
		}
		
		return list.get(0);
	}

	@Override
	public List<TeamDZBean> getTeamDzList( Long lianSaiId, Long lianSaiYearId , int lunci ) {

		DetachedCriteria criteria = DetachedCriteria.forClass( TeamDZBean.class, "teamDZ" );
		criteria.add( Restrictions.eq( "teamDZ.liansaiYear.id", lianSaiYearId ) );
		criteria.add( Restrictions.eq( "teamDZ.liansai.id", lianSaiId ) );
		
		criteria.add( Restrictions.eq( "teamDZ.lunci", lunci ) );
		
		criteria.addOrder( Order.asc( "teamDZ.playTime" ) );
		
		return this.teamDZDao.findByCriteria(criteria);
	}
	
	public List<TeamDZBean> getTeamDzList( Long lianSaiId,Long lianSaiYearId ){
		
		DetachedCriteria criteria = DetachedCriteria.forClass( TeamDZBean.class, "teamDZ" );
		criteria.add( Restrictions.eq( "teamDZ.liansaiYear.id", lianSaiYearId ) );
		criteria.add( Restrictions.eq( "teamDZ.liansai.id", lianSaiId ) );
		
		criteria.add( Restrictions.eq( "teamDZ.cjStatus", "已采集" ) );
		
		criteria.addOrder( Order.asc( "teamDZ.playTime" ) );
		
		return this.teamDZDao.findByCriteria(criteria);
		
	}
	
	public TeamDZBean getTeamDZBean( String playId ){
		return this.teamDZDao.findUniqueObj( "playId", playId );
	}
}
